package cn.tedu.supermarket.controller;

import cn.tedu.supermarket.entity.Address;
import cn.tedu.supermarket.entity.CartDetail;
import cn.tedu.supermarket.entity.User;
import cn.tedu.supermarket.mapper.AddressMapper;
import cn.tedu.supermarket.mapper.CartMapper;
import cn.tedu.supermarket.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class CartController {
    @Autowired
    ICartService cartService;
    @Autowired
    CartMapper cartMapper;
    @Autowired
    AddressMapper addressMapper;


    //验证是否登录
    @RequestMapping("/detail/check")
    @ResponseBody
    public int checkSession(HttpSession session){
        if (session.getAttribute("user")==null) {
            System.err.println("请先登录");
            return 0;
        }
        return 1;
    }
    //添加购物车
    @RequestMapping("/detail/addCart")
    @ResponseBody
    public Integer addGoodsForCart(int id){
        System.err.println("添加购物车");
        HttpServletRequest request =((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpSession se=request.getSession();
        User user=(User) se.getAttribute("user");
        int arr[] = cartMapper.selectNumByUserId(user.getId());
        if (arr.length<10){
            Integer s = cartMapper.selectByUserId(id,user.getId());
            if (s==null){
                int rows = cartService.addCart(id);
                System.err.println("添加购物车成功");
                return rows;
            }else {
                int price = cartMapper.selPriceById(id);
                Integer a =cartMapper.updateNum(price,id,user.getId());
                System.err.println("修改购物车成功");
                return a;
            }
        }
        return 0;
    }

    //遍历显示购物车商品
    @RequestMapping("/cartInfo")
    public String cartIN(Model model,HttpSession session){
        if (session.getAttribute("user")!=null){
            HttpServletRequest request =((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            HttpSession se=request.getSession();
            User user=(User) se.getAttribute("user");
            List<CartDetail> list = cartService.cartDetail(user.getId());
            model.addAttribute("list",list);
            List<Address> list1 = addressMapper.selAddressByUserId(user.getId());
            model.addAttribute("list1",list1);
            return "buy_user/cart";
        }
        return "buy_user/buserLogin";
    }
    //删除商品
    @RequestMapping("/deleteCartGoods")
    public String del(int id){
        cartMapper.deleteById(id);
        return "redirect:cartInfo";
    }

    //变更商品数量
    @RequestMapping("/changeNum")
    public String addNum(int id,int num){
        Integer price = cartMapper.selectPriceByCartId(id);
        cartMapper.updateNumById(num,num,price,id);
        return "redirect:cartInfo";
    }

}
